package com.hrm.controller;


import com.hrm.pojo.DTO.DeptPageDTO;
import com.hrm.pojo.Dept;
import com.hrm.service.DeptService;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dept")
@RestController
@Api(tags = "部门相关接口")
public class DeptController {

    @Autowired
    private DeptService deptService;
    /**
     * 部门查询
     * @param deptPageDTO
     * @return
     */
    @ApiOperation("部门查询")
    @GetMapping("/page")
    public Result page(DeptPageDTO deptPageDTO){
       PageResult<Dept> pageDept = deptService.page(deptPageDTO);
        return new Result(pageDept!=null,pageDept!=null? StatusCode.SUCCESS:StatusCode.ERROR,"操作完成",pageDept);

    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @ApiOperation("删除部门")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        Boolean flag = deptService.deleteById(id);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @ApiOperation("添加部门")
    @PostMapping("/add")
    public Result addDept(@RequestBody Dept dept){
        Boolean flag = deptService.insertDept(dept);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    /**
     * 修改部门
     * dept
     * @return
     */
    @ApiOperation("修改部门")
    @PutMapping("/update")
    public Result updateDept(@RequestBody Dept dept){
        Boolean flag = deptService.updateById(dept);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    @ApiOperation("获得所有部门信息")
    @GetMapping("/options")
    public Result getAllDept(){
        List<Dept> list = deptService.getAllDept();
        return new Result(list!=null, list!=null ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成",list);
    }
}
